/*
Copyright (c) 2010 Andreas Bjerkeholt
License: MIT
http://www.opensource.org/licenses/mit-license.php
*/
#ifndef __mixer_h__
#define __mixer_h__

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <dingoo/ucos2.h>
#include <dingoo/audio.h>
#include <ctype.h>

#include "tremor/ivorbisfile.h"
#include "tremor/ivorbiscodec.h"
#include "tremor/ivorbisbuffer.h"

#define min(x, y) (x < y ? x : y)

#if defined(GEMEI_760P)
#define VOLUME_MAX 100
#define VOLUME_STEP 5
#else
#define VOLUME_MAX 30
#define VOLUME_STEP 2
#endif

#if defined(GEMEI_A330)
#ifndef SYSSetVolume
extern void SYSSetVolume(int vol);
#endif
#endif

typedef struct {
	void*          buffer;
	unsigned int   size;
	unsigned int   index;
	bool           repeat;
} playingSound;

#define  TASK_START_PRIO 5
#define  TASK_STK_SIZE   1024                   // Size of each task's stacks (# of WORDs)
#define  NO_TASKS        1                      // Number of identical tasks 

#define MAXFRAGSIZE 1024
#define SOUNDBUFFERS 10

extern vorbis_info* oggInfo;
extern uint32_t oggSongLength;
extern uint32_t oggSongCur;

extern uint32_t mixerSampleRate;
extern uint8_t mixerBits;
extern uint8_t mixerChannels;
extern int8_t mixerVolume;

extern char _lastError[];

void mixerInit(unsigned int samplerate, unsigned short bits, unsigned char channels, unsigned char volume);
void mixerClose();
void mixerPause(bool b);
bool mixerIsPaused();
int8_t mixerGetVolume();
void mixerSetVolume(int8_t v);
void mixerVolumeIncrease();
void mixerVolumeDecrease();
void mixerSetSampleRate(uint16_t rate);
void mixerSetChannels(uint8_t channels);
void mixerSetBits(uint8_t bits);
void mixerStopAllSound();
void _mixerPlay(void *none);
void mixerSetSound(void* buffer, unsigned int size, bool repeat);
void mixerOggClear();
bool mixerSetOgg(char* file);
bool mixerSetOggFromBuffer(unsigned char* buffer, int size);
bool mixerUpdate();
void _mixerUpdateOgg();
void _mixerUpdateRaw();
char* mixerLastError();

#endif
